﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DECOR.BL.BE;
using DECOR.BL.BC;
using Newtonsoft.Json;

namespace DECOR.Touchapp
{
    public partial class stock : System.Web.UI.Page
    {
        string pRetval = "", json = "";
        List<StockCursoBE> lstSC;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.HttpMethod.Equals("POST"))
            {
                
            }
            else
            {
                CredencialesController.cargarInfo(Request.Params); //(SIEMPRE)
                string p_matnr = Request.Params["MATNR"] != null ? Request.Params["MATNR"].ToString() : "";
                string p_maxtx = Request.Params["MAXTX"] != null ? Request.Params["MAXTX"].ToString() : "";
                string p_vbeln = Request.Params["VBELN"] != null ? Request.Params["VBELN"].ToString() : "";
                string p_lfdat_ini = Request.Params["LFDAT_INI"] != null ? Request.Params["LFDAT_INI"].ToString() : "";
                string p_lfdat_fin = Request.Params["LFDAT_FIN"] != null ? Request.Params["LFDAT_FIN"].ToString() : "";

                pRetval = "{\"success\": {0}, \"errors\":{\"reason\": \"{1}\"}, \"lstStockCurso\":{2} ";
                pRetval += "}";

                lstSC = StockBC.Instance.buscaStockECTRansito(p_matnr, p_maxtx, p_vbeln, Convert.ToDateTime(p_lfdat_ini), Convert.ToDateTime(p_lfdat_fin));
                if (lstSC != null || lstSC.Count > 0)
                {
                    json = JsonConvert.SerializeObject(lstSC);
                    pRetval = pRetval.Replace("{0}", "true");
                    pRetval = pRetval.Replace("{1}", "");
                    pRetval = pRetval.Replace("{2}", json);
                }
                else
                {
                    pRetval = pRetval.Replace("{0}", "false");
                    pRetval = pRetval.Replace("{1}", "No se encontraron los registros para los parámetros seleccionados.");
                    pRetval = pRetval.Replace("{2}", "[]");
                }
            }

            Response.ContentType = "application/json; charset=UTF-8";
            Response.Write(pRetval);
        }
    }
}
